<template>
  <div>
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :before-close="close">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" class="demo-ruleForm">

        <!--        <el-form-item label="用途" prop="type">-->
        <!--          <el-radio v-model="form.type" label="0">仓库</el-radio>-->
        <!--          <el-radio v-model="form.type" label="1">新闻</el-radio>-->
        <!--        </el-form-item>-->

        <el-form-item label="平台名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-row style="top: 30px;margin-bottom: 30px;text-align:center;">
          <el-col :span="24">
            <el-button type="primary" size="medium" @click="submitForm('form')">{{ title }}</el-button>
            <el-button size="medium" @click="close()">取消</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { businesstypeAdd, businesstypeEdit } from '@/api/basics'
import { platformAdd, platformEdit } from '@/api/newsDeclare'
export default {
  name: 'AddEdit',
  props: {
    open: {
      type: Boolean
    },
    form: {
      type: Object
    },
    title: {
      type: String
    }
  },
  data() {
    return {
      rules: {
        name: [
          { required: true, message: '请填写平台名称', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    'open': function(newValue, oldValue) {
      if (newValue) {
        if (this.title === '编辑') {
          this.form.type = this.form.type.toString()
        } else {
          // this.form.type = '1'
        }
      }
    }
  },
  methods: {
    submitForm(formName) {
      const that = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (that.title === '添加') {
            this.add()
          } else {
            this.edit()
          }
        } else {
          return false
        }
      })
    },
    add() {
      const that = this
      platformAdd(that.form).then(response => {
        if (response.code === 10000) {
          that.$message.success(response.message)
          that.$parent.getList()
          that.$emit('update:open', false)
        }
      })
    },
    edit() {
      const that = this
      platformEdit(that.form).then(response => {
        if (response.code === 10000) {
          that.$message.success(response.message)
          that.$parent.getList()
          that.$emit('update:open', false)
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    close() {
      this.$emit('update:open', false)
    }
  }
}
</script>

